package com.bootdo.appsystem.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.appsystem.service.BannerService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.SellercategoryDO;
import com.bootdo.appsystem.service.SellercategoryService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/sellercategory")
public class SellercategoryController {
	@Autowired
	private SellercategoryService sellercategoryService;
	@Autowired
	private BannerService bannerService;
	@Autowired
	private BootdoConfig bootdoConfig;
	@Autowired
	private FileService sysFileService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:sellercategory:sellercategory")
	String Sellercategory(){
	    return "appsystem/sellercategory/sellercategory";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:sellercategory:sellercategory")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SellercategoryDO> sellercategoryList = sellercategoryService.list(query);
		int total = sellercategoryService.count(query);
		PageUtils pageUtils = new PageUtils(sellercategoryList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("appsystem:sellercategory:add")
	String add(){
	    return "appsystem/sellercategory/add";
	}

	@GetMapping("/edit/{secid}")
	@RequiresPermissions("appsystem:sellercategory:edit")
	String edit(@PathVariable("secid") Integer secid,Model model){
		SellercategoryDO sellercategory = sellercategoryService.get(secid);
		model.addAttribute("sellercategory", sellercategory);
	    return "appsystem/sellercategory/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:sellercategory:add")
	public R save( SellercategoryDO sellercategory){
		if(sellercategoryService.save(sellercategory)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:sellercategory:edit")
	public R update( SellercategoryDO sellercategory){
		sellercategoryService.update(sellercategory);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:sellercategory:remove")
	public R remove( Integer secid){
		if(sellercategoryService.remove(secid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:sellercategory:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] secids){
		sellercategoryService.batchRemove(secids);
		return R.ok();
	}

	/**
	 * 上传图片
	 */
	@PostMapping( "/upload")
	@ResponseBody
	public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName);
		FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/icons/" + fileName, new Date());
		try {
			FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath()+"/icons/", fileName);
		} catch (Exception e) {
			return R.error();
		}
		if (sysFileService.save(sysFile) > 0) {
			return R.ok().put("fileName",sysFile.getUrl());
		}
		return R.error();
	}
}
